package com.yowasa.xiuxian.engine.message.interact;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.annoations.Item;
import com.yowasa.xiuxian.engine.CommonEngine;
import com.yowasa.xiuxian.engine.item.ItemInterface;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.item.UserItemBo;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.enums.ItemTypeEnum;
import com.yowasa.xiuxian.entity.enums.MapEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.entity.po.XiuxianShopItem;
import com.yowasa.xiuxian.entity.po.XiuxianTravelShop;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.mapper.XiuxianShopItemMapper;
import com.yowasa.xiuxian.mapper.XiuxianTravelShopMapper;
import com.yowasa.xiuxian.service.helper.ItemHelper;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.MessageFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@CMD(name = "/游商", scope = CmdScopeEnum.ALL)
public class TransactTravelHandler extends CmdMessageHandler {

    @Autowired
    private UserHelper userHelper;
    @Autowired
    private MessageHelper messageHelper;
    @Autowired
    private ItemHelper itemHelper;

    @Autowired
    private XiuxianTravelShopMapper xiuxianTravelShopMapper;


    @Override
    public void execute(SendMessage message) {
        UserBO userBO = userHelper.checkOpUser(message);
        MapEnum map = MapEnum.getById(userBO.getLocal());

        List<String> params = getAllParam(message);//获取名称
        if (StringUtils.isEmpty(params.get(0))) {
            messageHelper.replyMys(message, "可以在后面追加 查询 购买 等二级指令进行后续操作");
            return;
        }
        if ("查询".equals(params.get(0))) {
            List<XiuxianTravelShop> xiuxianTravelShops = xiuxianTravelShopMapper.selectList(new LambdaQueryWrapper<XiuxianTravelShop>()
                    .gt(XiuxianTravelShop::getEndTime,new Date())
                    .eq(XiuxianTravelShop::getMapId,userBO.getLocal()));
            String msg = "[" + map.getName() + "游商]\n" + buildBatchTransactMsg(xiuxianTravelShops);

            messageHelper.replyMys(message, msg);
            return;
        }

        if ("购买".equals(params.get(0))) {
            if (params.size() < 2) {
                throw new HandlerException("请在购买后输入 物品名 数量 不输入数量默认为1");
            }
            String itemName = params.get(1);
            int num = 1;
            if (params.size() >= 3) {
                num = StrUtil.checkAndConvert2Int(params.get(2), "数量");
            }
            List<XiuxianTravelShop> xiuxianTravelShops = xiuxianTravelShopMapper.selectList(new LambdaQueryWrapper<XiuxianTravelShop>()
                    .gt(XiuxianTravelShop::getEndTime,new Date())
                    .eq(XiuxianTravelShop::getMapId,userBO.getLocal())
                    .eq(XiuxianTravelShop::getItemName,itemName)
                    .gt(XiuxianTravelShop::getItemNum,0).last("limit 1"));

            ItemInterface itemInterface = CommonEngine.ITEM_MAP.get(itemName);
            if (itemInterface == null || xiuxianTravelShops.size()==0) {
                throw new HandlerException("没有这样的物品 : " + itemName);
            }
            XiuxianTravelShop item = xiuxianTravelShops.get(0);

            if (item.getItemNum() < num) {
                throw new HandlerException("没有足够数量的购买物品，该物品现有上架数量为:" + item.getItemNum());
            }
            Integer cost = item.getItemPrice() * num;

            if (userBO.getUserConfig().getGold() < cost) {
                throw new HandlerException("你身上的摩拉不足，需要:" + cost);
            }
            //扣钱
            userBO.getUserConfig().setGold(userBO.getUserConfig().getGold() - cost);
            //物品增加
            if(itemInterface.getType().getId() == 3){
                itemHelper.addEquip(userBO,itemInterface,num,2);
            }else {
                itemHelper.addItem(userBO,itemInterface,num);
            }
            //商店物品减少
            item.setItemNum(item.getItemNum() - num);
            xiuxianTravelShopMapper.updateById(item);

            userHelper.saveUserInfo(userBO);
            messageHelper.replyMys(message, "购买成功");
            return;
        }

    }
    public String buildTransactMsg(XiuxianTravelShop po) {
        //武器【xxx】*100，单价100
        return MessageFormat.format("{0}[{1}]*{2},单价{3}", ItemTypeEnum.getById(po.getItemType()).getName(), po.getItemName(), po.getItemNum(), po.getItemPrice());
    }
    public String buildBatchTransactMsg(List<XiuxianTravelShop> po) {
        return po.stream().map(this::buildTransactMsg).collect(Collectors.joining("\n"));
    }

    public String desc(){
        return new StringBuffer().append("游商为奇遇刷新，璃月全境可触发\n")
                .append("游商的每次出现都会在那个地点持续一个小时\n游商仅售卖两件物品，必定出现一件高级物品\n")
                .toString();

    }
}
